package hevs.activities;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import hevs.greencontrol.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SelectorMapEdit extends Activity implements OnClickListener{

	// DATA
	private final static String LOGIN="login.txt";
	private TextView text;
	//private UserData userData;
	private Activity currentActivity;
	private AlertDialog helpDialog;
	private  Button ValidButton;
	
	 @Override
	    protected void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.selectormapedit);
	        
	        // set the current activity
	        currentActivity = this;
        
	        // instanciate the textView
	        text = (TextView) this.findViewById(R.id.name_room);
	        
	        ValidButton = (Button) this.findViewById(R.id.valid_button_room);
	        ValidButton.setOnClickListener(this);
	        
	        helpDialog = new AlertDialog.Builder(this).create();
	    }
	 
	//***********************************************************************************************  
		// Option menu :
		//*********************************************************************************************** 

		/**
		 * Create the option menu
		 */
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			
			menu.add(0, 1000, 0, "Logout").setIcon(android.R.drawable.ic_menu_close_clear_cancel) ;

			return super.onCreateOptionsMenu(menu);
		}

		/**
		 * Switch the actions when executed on menu item click
		 */
		@Override
		public boolean onOptionsItemSelected(MenuItem item){
			switch (item.getItemId()) {

			case 1000:
				//Logout :
				logout();
				startActivity(new Intent(this,StartPage.class));
				this.finish(); 
				break;

			default:
				break;
			}

			return super.onOptionsItemSelected(item);
		}
		
		/**
		 * Method used to logout the application
		 */
		private void logout()
		{	
			try
			{
				// We get the username 
				InputStream inputStream = openFileInput(LOGIN);
				
				String un = "";
				String pw = "";
				
				if (inputStream != null)
				{
					InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
					BufferedReader reader = new BufferedReader(inputStreamReader);
		
					// read automatic login
					reader.readLine();
					
					// read username
					un = reader.readLine();
					
					// read password
					pw = reader.readLine();
		
					inputStream.close();
					
					// Now we set the automatic login to false and we rewrite the username
					OutputStreamWriter output = new OutputStreamWriter(openFileOutput(LOGIN, 0));
					output.write("false" + "\n" + un + "\n" + pw);
					output.close();
				}
			}
			catch (java.io.FileNotFoundException e)
			{
				// file login.txt not created
			}
			catch (Throwable t)
			{
				Toast.makeText(this, "Exception : " + t.toString(), 2000).show();
			}
		}
	        
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == ValidButton)
		{
			startActivity(new Intent(this,Selector.class));
		}
		
	}

}
